package com.joe.mvvm.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.example.kotlindemo.R
import com.example.kotlindemo.databinding.ActivityMvvmDemoBinding
import com.joe.mvvm.viewmodel.APIViewModel


/**
 * MVVM Demo Activity
 *
 * @author zhouronghua
 * @time 2022/3/27 4:08 下午
 */
class MvvmDemoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 初始化视图页面
        var binding = DataBindingUtil.setContentView<ActivityMvvmDemoBinding>(
            this,
            R.layout.activity_mvvm_demo
        )
        // 绑定生命周期
        binding.lifecycleOwner = this
        val viewModel = ViewModelProvider(
            this,
            ViewModelProvider.NewInstanceFactory()
        ).get(APIViewModel::class.java)
        // 设置ViewModel
        binding.vm = viewModel
        binding.btnNetRequest.setOnClickListener {
            // 调用网络登录请求
            viewModel.requestLogin("Derry-vip", "123456")
        }
    }
}